<?php
/**
 * Nweb Image Manager
 *
 * This script is protected by copyright. It's use, copying, modification
 * and distribution without written consent of the author is prohibited.
 *
 * @category    source
 * @package     pl.nweb.image.adapter
 * @author      Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright   Copyright (c) 2011 Krzysztof Kardasz
 * @license     http://www.gnu.org/licenses/lgpl-3.0.txt  GNU Lesser General Public
 * @version     3.0 $Id: binary.php 105 2012-02-24 09:01:04Z krzysztof.kardasz $
 * @link        http://code.google.com/p/nweb-framework/
 * @link        http://framework.nweb.pl
 */

namespace pl\nweb\image\adapter\gd;

/**
 * Utworzenie obrazu z danych binarnych
 *
 * @category    source
 * @package     pl.nweb.image.adapter
 * @author      Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright   Copyright (c) 2011 Krzysztof Kardasz
 * @version     3.0 $Id: binary.php 105 2012-02-24 09:01:04Z krzysztof.kardasz $
 */
class Binary extends \pl\nweb\image\adapter\Gd
{
    /**
     * Konstruktor
     *
     * @param string $file ścieżka do pliku
     * @param integer $type typ obrazu
     * @return void
     * @throws \pl\nweb\image\adapter\gd\Exception
     */
    public function __construct($data, $type = self::TYPE_JPEG)
    {
        $this->_im = @imagecreatefromstring($data);
        if (!$this->_im) {
            $this->_im = null;
            throw new Exception('Damaged image data string', self::EXCEPTION_DAMAGED_DATA);
        }

        if (!in_array($type, array(self::TYPE_JPEG, self::TYPE_PNG, self::TYPE_GIF))) {
            throw new Exception('Not supported image type "' . $type . '"', self::EXCEPTION_UNSUPPORTED);
        }

        $obj = new self;
        $obj->_x    = imagesx($obj->_im);
        $obj->_y    = imagesy($obj->_im);
        $obj->_type = $type;

        if ($obj->_type == self::TYPE_PNG) {
            imagealphablending($obj->_im, false);
            imagesavealpha($obj->_im, true);
        }

        return $obj;
    }
}